<template>
  <div class="app-container">
    <el-form :inline="true" :model="form" class="demo-form-inline filter-container">
      <el-form-item prop="dateType">
        <el-date-picker
          v-model="form.time"
          class="filter-item"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item v-if="roleCheck('virtual')" class="with-pre-select" label="数据类型">
        <el-select v-model="form.isFake">
          <el-option v-for="item in consts.STATISTICS_RANGE"
                     :key="item.value"
                     :label="item.title"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-button type="primary" v-waves icon="el-icon-search" @click="submitFilter">搜索</el-button>
    </el-form>

    <el-table :data="tableData" v-loading="loading" element-loading-text="给我一点时间" border fit
              highlight-current-row
              style="width: 100%">
      <el-table-column label="游戏平台" align="center">
        <template slot-scope="{row}">
          {{row.game}}
        </template>
      </el-table-column>
      <!-- <el-table-column label="子游戏" align="center">
        <template slot-scope="{row}">
          {{row.gameType | toConverter('GAME_TYPE')}}
        </template>
      </el-table-column> -->
      <el-table-column label="下注金额" align="center">
        <template slot-scope="{row}">
          {{row.amount | currency}}
        </template>
      </el-table-column>
      <el-table-column label="派彩金额" align="center">
        <template slot-scope="{row}">
          {{row.prize | currency}}
        </template>
      </el-table-column>
      <el-table-column label="返奖率" align="center">
        <template slot-scope="{row}">
          {{row.amount === 0 ? '0%' : (row.prize / row.amount)*100 | currency}}%
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { form, grid } from '@/build';

import {
  riskAwardRateListApi as listApi,
} from '@/services/api/risk';

export default {
  name: 'award-rate',

  mixins: [form, grid],

  props: ['id'],

  data() {
    // let times = _.toPeriod({
    //   start: [-1, 'days'],
    //   end: [0, 'days'],
    //   format: 'YYYY-MM-DD'
    // });
    // 本月时间本月1号至当前时间
    const monthTime = [moment().startOf('month').format('YYYY-MM-DD'), moment().format('YYYY-MM-DD')];

    return {
      tableData: [],
      form: {
        time: monthTime,
        isFake: 0,
      },
      filters: {
        stime: moment(monthTime[0]).valueOf(),
        etime: moment(monthTime[1]).valueOf() + 86399999,
        isFake: 0,
      },
    };
  },
  mounted() {
    this.bindGet(listApi, 'tableData', {
      action: 'getGrid',
      format: data => Object.values(data),
      sendingData: 'filters',
    });
    this.getAll();
  },
  methods: {
    submitFilter() {
      const { time, isFake } = this.form;
      let stime; let etime;
      if (!_.isUndefined(time) && _.isArray(time)) {
        stime = moment(time[0]).valueOf();
        etime = moment(time[1]).valueOf() + 86399999;
      }

      this.filters = {
        ...this.filters, stime, etime, isFake,
      };
      this.gridSearch();
    },
  },
};
</script>
